#include "App/app_key.h"
#include "App/app_ui.h"
#include "App/app_main.h"

#include "BSP/KEY/key.h"

#include "Middlewares/FREERTOS/include/FreeRTOS.h"
#include "Middlewares/FREERTOS/include/task.h"

#include "Middlewares/Mini_UI/core/mini_ui.h"

static uint8_t is_app_key_task_create;

void app_key_task(void *arg)
{
    TickType_t tick = xTaskGetTickCount();
    is_app_key_task_create = 1;
    while (is_app_key_task_create)
    {
        vTaskDelayUntil(&tick, 50);
        key_state_t key;
        key_get_state(&key); // 获取按键状态

        if (key.event == KEY_EVENT_NONE)
            continue; // 没有按键事件，直接返回

        // debug_printf("event:%d, value: %d\r\n", key.event, key.value);

        uint8_t mini_ui_msg = 0x00;

        if (flash_config.oled_x_reverse == 1 && flash_config.oled_y_reverse == 1) // 如果屏幕翻转了
        {
            if (key.value == 1)
                key.value = 3;
            else if (key.value == 3)
                key.value = 1;
            if (key.value == 2)
                key.value = 4;
            else if (key.value == 4)
                key.value = 2;
        }

        if (mini_ui_state == UI_STATE_MENU)
        {
            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 1)
                mini_ui_msg = UI_MSG_UP;

            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 3)
                mini_ui_msg = UI_MSG_DOWN;

            if ((key.event == KEY_EVENT_CLICK) && key.value == 5)
                mini_ui_msg = UI_MSG_ENTER;

            if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 5)
                mini_ui_msg = UI_MSG_BACK;
        }
        else if (mini_ui_state == UI_STATE_SLIDER)
        {
            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 1)
                mini_ui_msg = UI_MSG_SLIDER_ADD;

            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 3)
                mini_ui_msg = UI_MSG_SLIDER_SUB;

            if ((key.event == KEY_EVENT_CLICK) && key.value == 5)
                mini_ui_msg = UI_MSG_SLIDER_OK;
        }
        else if (mini_ui_state == UI_STATE_SELECTOR)
        {
            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 1)
                mini_ui_msg = UI_MSG_SELECTOR_ADD;

            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 3)
                mini_ui_msg = UI_MSG_SELECTOR_SUB;

            if ((key.event == KEY_EVENT_CLICK) && key.value == 5)
                mini_ui_msg = UI_MSG_SELECTOR_OK;
        }
        else if (mini_ui_state == UI_STATE_FILE_BROWSER)
        {
            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 1)
                mini_ui_msg = UI_MSG_FILE_BROWSER_UP;

            if ((key.event == KEY_EVENT_CLICK || key.event == KEY_EVENT_HOLD) && key.value == 3)
                mini_ui_msg = UI_MSG_FILE_BROWSER_DOWN;

            if ((key.event == KEY_EVENT_CLICK) && key.value == 5)
                mini_ui_msg = UI_MSG_FILE_BROWSER_ENTER;

            if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 5)
                mini_ui_msg = UI_MSG_FILE_BROWSER_BACK;
        }
        else if (mini_ui_state == UI_STATE_POPUP)
        {
        }
        else if (mini_ui_state == UI_STATE_USER)
        {
            if ((key.event == KEY_EVENT_CLICK) && key.value == 1)
                mini_ui_msg = UI_MSG_UP_CLICK;
            else if ((key.event == KEY_EVENT_CLICK) && key.value == 3)
                mini_ui_msg = UI_MSG_DOWN_CLICK;
            else if ((key.event == KEY_EVENT_CLICK) && key.value == 2)
                mini_ui_msg = UI_MSG_LEFT_CLICK;
            else if ((key.event == KEY_EVENT_CLICK) && key.value == 4)
                mini_ui_msg = UI_MSG_RIGHT_CLICK;
            else if ((key.event == KEY_EVENT_CLICK) && key.value == 5)
                mini_ui_msg = UI_MSG_MIDDLE_CLICK;

            else if ((key.event == KEY_EVENT_DOUBLE_CLICK) && key.value == 1)
                mini_ui_msg = UI_MSG_UP_DOUBLE_CLICK;
            else if ((key.event == KEY_EVENT_DOUBLE_CLICK) && key.value == 3)
                mini_ui_msg = UI_MSG_DOWN_DOUBLE_CLICK;
            else if ((key.event == KEY_EVENT_DOUBLE_CLICK) && key.value == 2)
                mini_ui_msg = UI_MSG_LEFT_DOUBLE_CLICK;
            else if ((key.event == KEY_EVENT_DOUBLE_CLICK) && key.value == 4)
                mini_ui_msg = UI_MSG_RIGHT_DOUBLE_CLICK;
            else if ((key.event == KEY_EVENT_DOUBLE_CLICK) && key.value == 5)
                mini_ui_msg = UI_MSG_MIDDLE_DOUBLE_CLICK;

            else if ((key.event == KEY_EVENT_HOLD) && key.value == 1)
                mini_ui_msg = UI_MSG_UP_HOLD;
            else if ((key.event == KEY_EVENT_HOLD) && key.value == 3)
                mini_ui_msg = UI_MSG_DOWN_HOLD;
            else if ((key.event == KEY_EVENT_HOLD) && key.value == 2)
                mini_ui_msg = UI_MSG_LEFT_HOLD;
            else if ((key.event == KEY_EVENT_HOLD) && key.value == 4)
                mini_ui_msg = UI_MSG_RIGHT_HOLD;
            else if ((key.event == KEY_EVENT_HOLD) && key.value == 5)
                mini_ui_msg = UI_MSG_MIDDLE_HOLD;

            else if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 1)
                mini_ui_msg = UI_MSG_UP_LONG;
            else if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 3)
                mini_ui_msg = UI_MSG_DOWN_LONG;
            else if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 2)
                mini_ui_msg = UI_MSG_LEFT_LONG;
            else if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 4)
                mini_ui_msg = UI_MSG_RIGHT_LONG;
            else if ((key.event == KEY_EVENT_LONG_PRESS) && key.value == 5)
                mini_ui_msg = UI_MSG_MIDDLE_LONG;
        }

        xTaskNotify(xTaskGetHandle("app_ui_task"), (uint32_t)mini_ui_msg, eSetValueWithOverwrite);
    }
    vTaskDelete(NULL);
}

/**
 * @brief       启动按键处理任务
 * @param       无
 * @retval      1:失败 0:成功
 */
uint8_t app_key_task_start(void)
{
    if (is_app_key_task_create != 0)
    {
        return 1;
    }

    if (xTaskCreate(app_key_task, "app_key_task_start", 32, NULL, 1, NULL) != pdPASS)
    {
        return 1;
    }
    return 0;
}

/**
 * @brief       停止按键处理任务
 * @param       无
 * @retval      无
 */
void app_key_task_stop(void)
{
    is_app_key_task_create = 0;
}
